package com.xlh.service.file.impl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.RandomUtil;
import com.xlh.exception.common.GlobalException;
import com.xlh.properties.PandocProperties;
import com.xlh.service.file.FileService;
import com.xlh.util.PandocFileUtil;
import com.xlh.util.WkhtmltopdfUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

/**
 * @author zhr 2019/11/15
 */
@Service
public class FileServiceImpl implements FileService {

    private static Logger logger = LoggerFactory.getLogger(FileServiceImpl.class);

    @Autowired
    protected PandocProperties pandocProperties;

    @Override
    public String word2markdown(MultipartFile file) {
        // 检查上传的文件是否为docx
        String fileName = file.getOriginalFilename();
        String extension = fileName.substring(fileName.lastIndexOf("."));
        if (!Objects.equals(extension, ".docx")) {
            throw new GlobalException("请上传07版本word文档");
        }

        // 将word文件保存至服务器上
        String filePath = pandocProperties.getTemp() + RandomUtil.simpleUUID() + ".docx";
        saveTemp(file, filePath);
        String content = "";

        try {
            // 通过PandocFileUtil将docx转为markdown
            content = PandocFileUtil.docx2gfm(filePath);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new GlobalException("转换失败");
        } finally {
            // 移除临时文件包
            FileUtil.del(pandocProperties.getTemp());
        }

        return content;
    }

    @Override
    public String html2pdf(String pdfPath, String content) {
        String htmlPath = pandocProperties.getTemp() + RandomUtil.simpleUUID() + ".html";
        saveTemp(htmlPath, content);

        try {
            // 创建空文件防止路径不存在
            FileUtil.touch(pdfPath);
            logger.info("============"+content+"============");
            logger.info("============"+htmlPath+"============");
            // 通过PandocFileUtil将docx转为markdown
            if (!WkhtmltopdfUtil.html2pdf(htmlPath, pdfPath)){
                logger.error("==========转换失败的html========" + content);
                throw new GlobalException("转换失败");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            // 移除临时文件包
            // FileUtil.del(pandocProperties.getTemp());
        }

        return null;
    }


    private void saveTemp(MultipartFile file, String filePath) {
        try {
            FileUtil.touch(filePath);
            File tempFile = new File(filePath);
            FileUtil.writeBytes(file.getBytes(), tempFile);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
            throw new GlobalException("操作失败");
        }
    }

    private void saveTemp(String filePath, String content) {
        try {
            FileUtil.touch(filePath);
            File tempFile = new File(filePath);
            FileUtil.writeUtf8String(content, tempFile);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new GlobalException("操作失败");
        }
    }
}
